<?php
namespace org\kahikai\database\mysql;

require_once dirname(__FILE__) . '/../AliasDao.php';
require_once dirname(__FILE__) . '/KahikaiMysql.php';
require_once dirname(__FILE__) . '/../../domain/Alias.php';

use org\kahikai\database\AliasDao;
use org\kahikai\database\LanguageDao;
use org\kahikai\domain\Alias;

/**
 * 
 * @author ch4mp
 *
 */
class MysqlAliasDao implements AliasDao {
	private $languageDao;
	
	public function __construct(LanguageDao $aLanguageDao) {
		$this->languageDao = $aLanguageDao;
	}
	
	/**
	 *
	 * @param int $geneId a Species id
	 * @return an array with all Alias for provided Gene (never null even if species id is unknown)
	 */
	public function getByGeneId($geneId) {
		$aliases = array();
		
		$result = KahikaiMysql::query("SELECT language_id, name FROM GENE_ALIAS WHERE gene_id=$geneId");
		while ($record = $result->fetch_assoc()) {
			array_push($aliases, new Alias($record['name'], $this->languageDao->getById($record['language_id'])));
		}
		$result->close();
		
		return $aliases;
	}
	
	/**
	 *
	 * @param int $speciesId a Species id
	 * @return an array with all Alias for provided Species (never null even if species id is unknown)
	 */
	public function getBySpeciesId($speciesId) {
		$aliases = array();
		
		$result = KahikaiMysql::query("SELECT language_id, name FROM SPECIES_ALIAS WHERE species_id=$speciesId");
		while ($record = $result->fetch_assoc()) {
			array_push($aliases, new Alias($record['name'], $this->languageDao->getById($record['language_id'])));
		}
		$result->close();
		
		return $aliases;
	}
	
}
?>